package com.lei.zjs.web.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.lei.zjs.service.ebi.StaffService;
import com.lei.zjs.vo.PageBean;
import com.lei.zjs.vo.Staff;
import com.lei.zjs.web.action.base.BaseAction;

/**
 * 取派员管理Action
 * @author Spinacia
 *
 */
@Controller
@Scope("prototype")
public class StaffAction extends BaseAction<Staff> {

	@Resource
	private StaffService staffService;
	
	//保存取派员
	public String save(){
		staffService.save(model);
		return "list";
	}
	
	private Map map = new HashMap();
	public Map getMap() {
		return map;
	}

	//分页显示所有取派员信息
	public String showAll(){
		//分页的页码
		String page = ServletActionContext.getRequest().getParameter("page");
		//分页每页的数据量
		String rows = ServletActionContext.getRequest().getParameter("rows");
		if (null == page || "".equals(page)) {
			page = "1";
		}
		if (null == rows || "".equals(rows)) {
			rows = "10";
		}
		List<Staff> list = staffService.showAll(Integer.parseInt(page),Integer.parseInt(rows));
		Long count = staffService.getCount(); 
		
		map.put("total", count);
		map.put("rows", list);
		return "json";
	}
	
	//分页查询
	public String pageQuery(){
		staffService.pageQuery(pageBean);
		
		map.put("total", pageBean.getTotal());
		map.put("rows", pageBean.getRows());
		return "json";
	}
	
	private Staff staff;
	
	public Staff getStaff() {
		return staff;
	}

	//获取单个取派员信息
	public String getOneStaff(){
		staff = staffService.getStaff(model.getId());
		return "editAjax";
	}
	
	//修改取派员信息
	public String update(){
		staffService.update(model);
		return "list";
	}
	
	//删除取派员(逻辑删除)
	@RequiresPermissions("staff-delete")
	public String delete(){
		String ids = ServletActionContext.getRequest().getParameter("ids");
		staffService.delete(ids);
		return "list";
	}
	
	//还原取派员
	public String restore(){
		String ids = ServletActionContext.getRequest().getParameter("ids");
		staffService.restore(ids);
		return "list";
	}
	
	//按条件加载取派员信息
	public String findStaffByAjax() throws IOException{
		DetachedCriteria dc = DetachedCriteria.forClass(Staff.class);
		dc.add(Restrictions.eq("deltag", "0"));
		dc.add(Restrictions.isEmpty("decidedzones"));
		
		List<Staff> list = staffService.findByConditions(dc);
		
		String json = JSON.toJSONString(list);
		ServletActionContext.getResponse().setContentType("html/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(json);
		return NONE;
	}
	
	//加载所有可用的取派员
	public String ajaxlist() throws IOException{
		DetachedCriteria dc = DetachedCriteria.forClass(Staff.class);
		dc.add(Restrictions.eq("deltag", "0"));
		
		List<Staff> list = staffService.findByConditions(dc);
		
		String[] excludes = new String[]{"decidedzones","telephone","haspda","deltag","station","standard"};
		this.writeJsonArray2String(list, excludes );
		return NONE;
	}
}
